const { Controller } = require("egg");

class PersonCertificateController extends Controller {


    async list() {
        const { ctx, service } = this;

        const { pageNum = 1, pageSize = 20, search } = ctx.query;

        try {
            const result = await service.personCertificate.list({
                search,
                pageNum: this.ctx.helper.toInt(pageNum),
                pageSize: this.ctx.helper.toInt(pageSize)
            });
            this.ctx.helper.success(result);
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }

    async create() {
        const { ctx, service } = this;

        const { name, gender, idNumber, certificateName, certificateNumber, certificateLevel, workUnit, position } = ctx.request.body;
        try {
            const params = {
                name,
                gender,
                idNumber,
                certificateName,
                certificateNumber,
                certificateLevel,
                workUnit,
                position,
                isDeleted: 0,
            }
            const result = await service.personCertificate.create(params);
            this.ctx.helper.success(result);
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }

    async delete() {
        const { service } = this;

        const { id } = this.ctx.params;
        try {
            const deletedCount = await service.personCertificate.delete(id);
            if (deletedCount[0] > 0) {
                this.ctx.helper.success({ message: "删除成功" });
            } else {
                this.ctx.helper.error("记录不存在或已被删除");
            }
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }

    async update() {
        const { ctx, service } = this;
        const { id } = ctx.params;
        const body = ctx.request.body;

        // 确保isDeleted字段不会被更新覆盖，除非明确提供
        if (!body.hasOwnProperty('isDeleted')) {
            body.isDeleted = undefined; // 这样Sequelize不会尝试更新此字段
        }

        try {
            const [rows] = await service.personCertificate.update(id, body);
            if (rows > 0) {
                this.ctx.helper.success({ message: "更新成功" });
            } else {
                this.ctx.helper.error("记录不存在或已被删除");
            }
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }

    async bindPersonalCertificate() {
        const { ctx, service } = this;
        const { userId, certificateConfigId } = ctx.request.body;

        try {

            const { certificateName, certificateRules } = await service.certificateConfig.id(certificateConfigId);

            const certificateNumber = await service.personCertificate.findNextCertificate(certificateRules);

            const user = await service.user.getUser({ id: userId });

            const { gender, idNumber, workUnit, position, name } = user;

            const params = {
                name,
                gender,
                idNumber,
                workUnit,
                position,
                certificateName,
                certificateNumber,
            };
            const res = await service.personCertificate.create(params);
            this.ctx.helper.success(res);

        } catch (error) {
            throw new Error(error);
        }


    }
}

module.exports = PersonCertificateController;